package control;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CConnection {
  private final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
  private final String DATABASE = "jdbc:derby:snakes;create=true;";
	
  private Connection connection;
  private Statement statement;
  
  public void createConnection() throws SQLException, ClassNotFoundException {
    Class.forName(DRIVER);
    connection = DriverManager.getConnection(DATABASE);
  }
	
  public Connection getConnection() throws SQLException, ClassNotFoundException {
	if (connection == null)
      this.createConnection();
    
    return connection;
  }
  
  public Statement getStatement() throws SQLException, ClassNotFoundException {
	if (connection == null)
      this.createConnection();
	
	if (statement == null)
      this.statement = connection.createStatement();
	
	return statement;
  }
	
  public void closeConnection() throws SQLException {
    if (connection != null)
      connection.close();
  }
}
